<template>
  <div >
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/admin/role/list' }">角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加角色</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="add-role">
      <h3>添加角色</h3>
      <el-form :model="form" :rules="rules" ref="addRole" label-width="80px">
        <el-form-item label="英文名称"  prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="中文名称"  prop="display_name">
          <el-input v-model="form.display_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述"  >
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="add">立即创建</el-button>
          <el-button @click="reset">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: "RoleAdd",
    data(){
      return {
        form:{},
        rules:{
          name:[
            { required: true, message: '请输入英文名称', trigger: 'blur' },
            { min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: 'blur' }
          ],
          display_name:[
            { required: true, message: '请输入中文名称', trigger: 'blur' },
            { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
          ]
        },
      }
    },
    methods:{
      add(){
        this.$refs.addRole.validate(valid =>{
          if(valid){
            this.axios.post('/api/admin/role',this.form).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              // this.form = {}; // 清空表单数据
              this.$refs.addRole.resetFields()
            }).catch(err => {
              this.$message.error(err.response.data.message)
            });
          }
          else{
            this.$message.error("请填写完整信息")
          }
        });
      },
      reset(){
        this.$router.push('/admin/role/list')
      }
    }
  }
</script>

<style scoped lang="less">
  .add-role{
    width: 400px;
    margin-top: 20px;
  }
</style>
